package com.mrjy.video.bo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.VideoType;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.video.dao.IVideoTypeDao;
import com.mrjy.video.dto.VideoTypeDto;

@Service("videoTypeBo")
public class VideoTypeBo implements IVideoTypeBo {
	@Resource
	private IVideoTypeDao videoTypeDao;

	@Override
	public Object txInsert(Object object,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (StringUtils.isBlank(((VideoType)object).getTypeName())) {
			map.put("err_code", 400);
			map.put("err_msg", "标签名称不能为空");
			return map;
		}
		if (((VideoType)object).getTypeName().length()>16) {
			map.put("err_code", 401);
			map.put("err_msg", "标签名称不能超过16个字符");
			return map;
		}
		VideoType videoType = (VideoType)object;
		videoType.setDisableFlag(0);
		videoType.setCreateTime(new Timestamp(System.currentTimeMillis()));
		int row = videoTypeDao.insert(videoType);
		int id = videoType.getId();
		if (row>0) {
			map.put("err_code", 200);
			map.put("err_msg", "添加成功");
			map.put("typeId", id);
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "添加失败");
		}
		return map;
	}

	@Override
	public Object txDeleteByIds(int[] ids) {
		try {
			return videoTypeDao.deleteByIds(ids);
		} catch (DataAccessException e) {
			return 0;
		}
	}

	@Override
	public Object txUpdateDisableFlag(Object object,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (((VideoType)object).getIds()==null||((VideoType)object).getIds().length==0) {
			map.put("err_code", 400);
			map.put("err_msg", "请勾选要操作的记录");
			return map;
		}
		int row = videoTypeDao.updateDisableFlag(object);
		if (row>0) {
			map.put("err_code", 200);
			map.put("err_msg", "操作成功,"+row+"条记录受影响");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "操作失败,"+row+"条记录受影响");
		}
		return map;
	}

	@Override
	public Object txUpdateById(Object object,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (StringUtils.isBlank(((VideoType)object).getTypeName())) {
			map.put("err_code", 400);
			map.put("err_msg", "标签名称不能为空");
			return map;
		}
		if (((VideoType)object).getTypeName().length()>16) {
			map.put("err_code", 401);
			map.put("err_msg", "标签名称不能超过16个字符");
			return map;
		}
		int row = videoTypeDao.updateById(object);
		if (row>0) {
			map.put("err_code", 200);
			map.put("err_msg", "修改成功");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "修改失败");
		}
		return map;
	}

	@Override
	public Object queryById(int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("err_code", 0);
		map.put("err_msg", "查询成功");
		List<Object> list = new ArrayList<>();
		list.add(videoTypeDao.queryById(id));
		map.put("rows", list);
		return map;
	}
	
	@Override
	public List<Object> queryList(Object object) {
		return videoTypeDao.queryList(object);
	}
	
	//分页--后台系统
	@Override
	public Map<String, Object> queryPage(Object object,HttpSession session) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		VideoType videoType = (VideoType)object;
        map.put("total", videoTypeDao.countList(object));
		map.put("rows", videoTypeDao.queryList(object));
		return map;
	}

	@Override
	public List<Object> queryCanUseList() {
		return videoTypeDao.queryCanUseList();
	}

	@Override
	public Object queryPageList(VideoType videoType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", videoTypeDao.countList(videoType));
		map.put("rows", videoTypeDao.queryPageList(videoType));
		return map;
	}

	@Override
	public Object exportExl(VideoType videoType, HttpServletResponse response,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		List<Object> data = videoTypeDao.queryList(videoType);
		if (data == null || data.size()==0) {
			map.put("err_code", 404);
			map.put("err_msg", "无可导出数据");
			return map;
		}
		try {
			ExcelUtil.writeExcel(response, "标签"+DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR)+".xls", (List<VideoTypeDto>)(List)data, VideoTypeDto.class);
			map.put("err_code", 200);
			map.put("err_msg", "导出成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "发生异常");
		}
		return map;
	}

	@Override
	public Object getTotal(VideoType videoType) {
		return videoTypeDao.countList(videoType);
	}

	@Override
	public Object queryByDynamicFlag(VideoTypeDto dto) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (dto.getDynamicFlag()==null) {
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		List<VideoType> list = null;
		if (dto.getDynamicFlag()==1 && dto.getUserId()!=null) {  //是vip
			list = videoTypeDao.queryTabsByVip(dto);
		}
		if (dto.getDynamicFlag()==2 && dto.getUserId()!=null) {  //购买了课程包
			list = videoTypeDao.queryTabsByPaid(dto);
		}
		if(dto.getDynamicFlag()==3 && dto.getUserId()!=null){   //不是vip,没购买课程包,但有点击历史
			list = videoTypeDao.queryTabsByClickHistory(dto);
		}
		if (dto.getDynamicFlag()==4) {  //不是vip,没购买课程包,无点击历史, 则默认排序
			list = videoTypeDao.queryTabsByDefault(dto);
		}
		if (list!=null && list.size()>0) {
			map.put("err_code", 0);
			map.put("err_msg", "查询成功,有数据");
			map.put("rows", list);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "无数据");
		}
		return map;
	}


    @Override
    public Object queryStatisticsTabsByDefault(VideoTypeDto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (dto.getId() == null || dto.getUserId() == null) {
            map.put("err_code", 400);
            map.put("err_msg", "参数异常");
            return map;
        }
        VideoTypeDto videoType = videoTypeDao.queryStatisticsTabsByDefault(dto);
        if (videoType != null) {
            map.put("err_code", 0);
            map.put("err_msg", "查询成功,有数据");
            map.put("videoType", videoType);
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "无数据");
        }
        return map;
    }

}
